package com.gaojinqi.base.core.util;

import com.google.common.collect.Maps;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Enumeration;
import java.util.Map;

/**
 * 获取http上下文相关对象
 *
 * @author gaojinqi
 * @version 1.0
 * @since 2020年04月20日
 */
public class HttpContext {

    /**
     * 获取当前session
     */
    public static HttpSession getSession() {
        if (getRequest() != null) {
            return getRequest().getSession();
        }
        return null;
    }

    /**
     * 获取当前response
     */
    public static HttpServletResponse getResponse() {
        RequestAttributes ra = RequestContextHolder.getRequestAttributes();
        if (ra != null) {
            ServletRequestAttributes sra = (ServletRequestAttributes) ra;
            return sra.getResponse();
        } else {
            return null;
        }
    }

    public static HttpServletResponse getResponseUrgently() {
        return getResponseUrgently(null);
    }

    /**
     * 获取当前response，获取不到则报错
     */
    public static HttpServletResponse getResponseUrgently(RuntimeException throwException) {
        HttpServletResponse response = getResponse();
        if (response == null) {
            if (throwException != null) {
                throw throwException;
            } else {
                throw new RuntimeException("response is not exist");
            }
        }
        return response;
    }

    /**
     * 获取当前请求 request
     */
    public static HttpServletRequest getRequest() {
        RequestAttributes ra = RequestContextHolder.getRequestAttributes();
        if (ra != null) {
            ServletRequestAttributes sra = (ServletRequestAttributes) ra;
            return sra.getRequest();
        } else {
            return null;
        }
    }

    public static HttpServletRequest getRequestUrgently() {
        return getRequestUrgently(null);
    }

    /**
     * 获取当前请求 request，获取不到则报错
     */
    public static HttpServletRequest getRequestUrgently(RuntimeException throwException) {
        HttpServletRequest request = getRequest();
        if (request == null) {
            if (throwException != null) {
                throw throwException;
            } else {
                throw new RuntimeException("request is not exist");
            }
        }
        return request;
    }

    /**
     * 获取请求的ip地址
     */
    public static String getIp() {
        HttpServletRequest request = HttpContext.getRequest();
        if (request == null) {
            return "127.0.0.1";
        } else {
            return request.getRemoteHost();
        }
    }

    /**
     * 获取所有请求的值
     */
    public static Map<String, String> getRequestParameters() {
        Map<String, String> values = Maps.newLinkedHashMap();
        HttpServletRequest request = HttpContext.getRequest();
        if (request == null) {
            return values;
        }
        Enumeration<String> enums = request.getParameterNames();
        while (enums.hasMoreElements()) {
            String paramName = enums.nextElement();
            String paramValue = request.getParameter(paramName);
            values.put(paramName, paramValue);
        }
        return values;
    }

    public static String getRequestParameter(String key) {
        HttpServletRequest request = HttpContext.getRequest();
        if (request == null) {
            return null;
        } else {
            return request.getParameter(key);
        }
    }

}
